package com.itfreer.power.service.power.strategy;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.power.dao.role.RoleDao;
import com.itfreer.power.entity.user.UserRoleEntity;
import com.itfreer.power.service.role.RoleDataCache;

/**
 * 用户角色关联数据提供接口
 */
@Component("MemoryRoleDataProvide")
public class MemoryRoleDataProvide implements RoleDataProvide {

	@Autowired
	private RoleDao roleDao;

	@Autowired(required = false)
	private RoleDataCache roleDataCache;

	@Override
	public List<String> getRoles(String userid) {
		List<UserRoleEntity> userRoles = roleDao.getRoles(userid);
		if (userRoles == null) {
			// 当当前用户没有角色时，可返回用户默认角色
			if (roleDataCache != null && roleDataCache.getUserDefulatRoles() != null) {
				return roleDataCache.getUserDefulatRoles();
			} else {
				return null;
			}
		}
		List<String> result = new ArrayList<String>();
		for (UserRoleEntity u : userRoles) {
			result.add(u.getRoleId());
		}
		return result;
	}
}